<template>
    <h2>添加权益等级页面</h2>
    <table border="1">
        <tr>
            <td>会员等级</td>
            <td>
                <select v-model="info.Rank_Id">
                    <option :value="0">--请选择--</option>
                    <option v-for="item in rank" :value="item.Rank_Id">--{{item.Rank_Name}}--</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>会员权益</td>
            <td>
                <select v-model="info.MR_Id">
                    <option :value="0">--请选择--</option>
                    <option v-for="item in rights" :value="item.MR_Id">--{{item.MR_Name}}--</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                 <input type="button" value="添加" @click="Add" class="btn btn-success"/>
            </td>
        </tr>
    </table>
</template>
<script setup lang="ts">

    import {ref,OnMounted} from 'vue';
    import axios from 'axios';

    OnMounted(()=>{
        RankInfo();
        RightsInfo();
    });

    //会员等级
    const rank=ref([{
        Rank_Id: 0,
        Rank_Name:""
    }]);

    const RankInfo=()=>{
        axios.get("https://localhost:7153/api/Member/ShowRankInfo/ShowRankInfo")
        .then(res=>{
            rank.value=res.data;
        })
        .catch(err=>{
            console.log(err);
        })
    }

    //会员权益
    const rights=ref([{
        MR_Id: 0,
        MR_Name:""
    }]);

    const RightsInfo=()=>{
        axios.get("https://localhost:7153/api/Member/ShowRightsInfo/ShowRightsInfo")
        .then(res=>{
            rights.value=res.data;
        })
        .catch(err=>{
            console.log(err);
        })
    }


    const info=ref({
        MR_Id: 0,
        Rank_Id: 0
    });


    const Add=()=>{
        axios.post("https://localhost:7153/api/Member/AddRankRights/AddRankRights",info.value)
        .then(res=>{
            if(res.data>0){
                console.log(res.data);
                alert('添加成功');
            }
            else if(res.data==-1){
                alert('等级权益已经存在');
                return;
            }
            else{
                alert('添加失败');
            }
        })
        .catch(err=>{
            console.log(err);
        })
    }

</script>
